package top.lixunda.ecommerce.server.good.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.business.BaseServiceImpl;
import top.lixunda.ecommerce.server.business.api.entity.cache.CacheUser;
import top.lixunda.ecommerce.server.good.api.entity.db.EnshrineStoreDO;
import top.lixunda.ecommerce.server.good.api.entity.db.StoreDO;
import top.lixunda.ecommerce.server.good.api.entity.dto.EnshrineStoreQueryDTO;
import top.lixunda.ecommerce.server.business.api.entity.dto.PageDTO;
import top.lixunda.ecommerce.server.good.mapper.EnshrineStoreMapper;
import top.lixunda.ecommerce.server.good.service.IEnshrineStoreService;
import top.lixunda.ecommerce.server.good.service.IStoreService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品收藏 服务实现类
 * </p>
 *
 * @author Xumda
 * @since 2020-01-06
 */
@Service
public class EnshrineStoreServiceImpl
        extends BaseServiceImpl<EnshrineStoreMapper, EnshrineStoreDO>
        implements IEnshrineStoreService {

    private final IStoreService storeService;

    @Autowired
    public EnshrineStoreServiceImpl(IStoreService storeService) {
        this.storeService = storeService;
    }

    @Override
    public Integer addEnshrineStore(List<Integer> ids) {
        if (ObjectValidators.isEmpty(ids)) {
            throw getAppException("参数不能为空");
        }

        List<EnshrineStoreDO> list = new ArrayList<>();
        EnshrineStoreDO enshrineStoreDO;
        CacheUser userInfoDO = currentUserCacheNotNull();

        // 根据id查询店铺名称
        List<StoreDO> storeList = storeService.list(
                new LambdaQueryWrapper<StoreDO>()
                        .in(StoreDO::getId, ids));

        Map<Integer, String> nameMap = new HashMap<>(storeList.size());
        for (StoreDO g : storeList) {
            nameMap.put(g.getId(), g.getName());
        }

        for (Integer id : ids) {
            enshrineStoreDO = new EnshrineStoreDO();
            enshrineStoreDO.setUserId(userInfoDO.getAccount());
            enshrineStoreDO.setUserName(userInfoDO.getFirstName() + userInfoDO.getLastName());
            enshrineStoreDO.setStoreId(id);
            enshrineStoreDO.setStoreName(nameMap.get(id));
            enshrineStoreDO.setTime(LocalDateTime.now());
            enshrineStoreDO.setUseable(1);
            list.add(enshrineStoreDO);
        }

        this.saveBatch(list);
        return list.size();
    }

    @Override
    public Integer removeEnshrineStore(List<Integer> ids) {
        if (ObjectValidators.isEmpty(ids)) {
            return 0;
        }

        Integer userId = currentUserCacheNotNull().getAccount();

        boolean success = this.remove(
                new LambdaUpdateWrapper<EnshrineStoreDO>()
                        .eq(EnshrineStoreDO::getUserId, userId)
                        .in(EnshrineStoreDO::getId, ids));

        if (!success) {
            throw getAppException("取消收藏失败");
        }
        return ids.size();
    }

    @Override
    public List<EnshrineStoreDO> queryAllEnshrineStore() {
        Integer userId = currentUserCacheNotNull().getAccount();
        return this.list(
                new LambdaQueryWrapper<EnshrineStoreDO>()
                        .eq(EnshrineStoreDO::getUserId, userId));
    }

    @Override
    public List<EnshrineStoreDO> queryEnshrineStoreWrapper(EnshrineStoreQueryDTO enshrineStoreQueryDTO) {
        Integer userId = currentUserCacheNotNull().getAccount();
        return this.list(
                enshrineStoreQueryDTO.toWrapper()
                        .eq(EnshrineStoreDO::getUserId, userId));
    }

    @Override
    public IPage<EnshrineStoreDO> queryEnshrineStorePage(PageDTO<EnshrineStoreDO, ?> pageDTO) {
        Integer userId = currentUserCacheNotNull().getAccount();
        return this.page(
                pageDTO.getPage(),
                pageDTO.getWrapper(wrapper -> wrapper.eq(EnshrineStoreDO::getUserId, userId))
        );
    }

    @Override
    public IPage<EnshrineStoreDO> queryEnshrineStorePageWrapper(PageDTO<EnshrineStoreDO, EnshrineStoreQueryDTO> pageDTO) {
        Integer userId = currentUserCacheNotNull().getAccount();
        return this.page(
                pageDTO.getPage(),
                pageDTO.getWrapper(
                        wrapper -> wrapper.eq(EnshrineStoreDO::getUserId, userId)));

    }

    @Override
    public EnshrineStoreDO queryById(Integer id) {
        if (ObjectValidators.isEmpty(id)) {
            throw getAppException("收藏id不能为空");
        }
        Integer userId = currentUserCacheNotNull().getAccount();
        return this.getOne(
                new LambdaQueryWrapper<EnshrineStoreDO>()
                        .eq(EnshrineStoreDO::getUserId, userId)
                        .eq(EnshrineStoreDO::getId, id), false);
    }

    @Override
    public List<EnshrineStoreDO> queryByIds(List<Integer> ids) {
        if (ObjectValidators.isEmpty(ids)) {
            throw getAppException("收藏id列表不能为空");
        }
        Integer userId = currentUserCacheNotNull().getAccount();
        return this.list(
                new LambdaQueryWrapper<EnshrineStoreDO>()
                        .eq(EnshrineStoreDO::getUserId, userId)
                        .in(EnshrineStoreDO::getId, ids));
    }

}
